package com.hnevc.listviewdemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";

    EditText us,ps;
    Button lg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        lg= (Button) this.findViewById(R.id.id_bth_login);
        us= (EditText) this.findViewById(R.id.id_et_userName);
        ps= (EditText) this.findViewById(R.id.id_et_password);

        lg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Login();
                    }
                }).start();


            }

            private void Login() {
                OkHttpClient client = new OkHttpClient.Builder()
                        .cookieJar(new MycCookie())
                        .build();
                FormBody formBody = new FormBody.Builder()
                        .add("userName", "tom")
                        .add("userPassword", "123")
                        .build();
                Request request = new Request.Builder()
                        .url("http://"+getServerIp()+":8080/RestaurantServer/user/doLogin2")
                        .post(formBody)
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    String data = response.body().string();
                    Log.e(TAG, "onClick: " + data);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent=new Intent();
                            intent.setClass(Main2Activity.this,MainActivity.class);
                            startActivity(intent);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        });
        ImageView img;
        img= (ImageView) this.findViewById(R.id.img);
        Picasso.with(this).load("http://e.hiphotos.baidu.com/image/pic/item/d01373f082025aaf10947801f2edab64024f1a51.jpg").into(img);
    }
    public void xg(View v){
        EditText ip= (EditText) this.findViewById(R.id.id_btn_saveip);
        String strip=ip.getText().toString();
        SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("serverip",strip);
        editor.commit();
    }
    public String getServerIp() {
        SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
        String serverip = sp.getString("serverip", null);
        return serverip;
    }
}
